package com.cnyz.utils;

import com.cnyz.exception.BaseException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.cnyz.constant.Constants.LOCAL_DISK_PREFIX;
import static com.cnyz.constant.Constants.URL_PREFIX;

public class FileUtil {

//    @Value("${spring.upload.whisper}")
//    private String whisperPath; // Python脚本路径

    public static String extractTextFromPdf(String filePath) {
        try{
            PDDocument pdDocument = PDDocument.load(new File(filePath));
            PDFTextStripper pdfStripper = new PDFTextStripper();
            String text=pdfStripper.getText(pdDocument);
            System.out.println("PDF文本是"+text);
            return text;
        }
        catch(Exception e){
            throw new BaseException("PDF阅读失败"+e.getMessage());
        }
    }//使用pdf来读取pdf文件输出文本

    public static String extractTextFromVideo(String filePath) {
        try{
            String PythonPath= "E:\\codeSource\\PycharmProjects\\whisper\\whisper-main\\test.py";
            String pythonExe = "E:\\codeSource\\PycharmProjects\\whisper\\.venv\\Scripts\\python.exe";
            ProcessBuilder pb = new ProcessBuilder(pythonExe,PythonPath,filePath);

            BufferedReader reader = new BufferedReader(new InputStreamReader(pb.start().getInputStream()));
            String line;
            StringBuilder result = new StringBuilder();
            while((line= reader.readLine()) != null){
                result.append(line);
                System.out.println("来自python脚本"+line);
            }
            return result.toString();


        }
        catch(Exception e){
            throw new BaseException("视频转音频失败");
        }
    }

    public static String urlToLocalPath(String url) {
        if (url.startsWith(URL_PREFIX)) {
            String relativePath = url.substring(URL_PREFIX.length());
            return LOCAL_DISK_PREFIX + relativePath.replace("/", "//");
        }
        throw new IllegalArgumentException("无效的URL路径：" + url);
    }//实现线上路径转本地路径

    public static void Mp4ToWav(String filePath){
        try {
            Path path = Paths.get(filePath);
            String audioFilePath = path.getParent().resolve(path.getFileName().toString().replaceFirst("[.][^.]+$", ".wav")).toString();//输出到同名的wav格式
            String ffmpegCommand = String.format("ffmpeg -i \"%s\" \"%s\"", filePath, audioFilePath);
            System.out.println("执行命令"+ffmpegCommand);
            System.out.println("MP4路径"+filePath);
            System.out.println("wav路径"+audioFilePath);
            Process process = new ProcessBuilder(ffmpegCommand).start();
            process.waitFor();
            System.out.print("成功转文件"+audioFilePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new BaseException("MP4转WAV失败"+e.getMessage());
        }
    }//实现mp4转wav的功能
}
